package com.zzyl.controller;

import com.github.pagehelper.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto"),
            @ApiImplicitParam(paramType = "path", name="pageNum",value = "页码",example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name="pageSize",value = "每页条数",example = "10", dataType = "Integer"),
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult<PageResponse<RoleDto>> List(@RequestBody RoleDto roleDto,
                                              @PathVariable("pageNum") Integer pageNum,
                                              @PathVariable("pageSize")Integer pageSize) {
        PageResponse<RoleVo> pageResponse = roleService.list(roleDto,pageNum,pageSize);
        return ResponseResult.success(pageResponse);
    }


    @PutMapping
    @ApiOperation("新增角色")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState"})
    public ResponseResult<RoleDto> add(@RequestBody RoleDto roleDto) {
        Boolean flag = roleService.add(roleDto);
        return ResponseResult.success(flag);
    }

    /**
     * 根据角色查询资源
     * @param roleId
     * @return
     */
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult<List<String>> findCheckedResources(@PathVariable("roleId") Object roleId) {
        if(roleId.equals("null")){
            return ResponseResult.success();
        }
        List<String> list =roleService.findCheckedResources(Long.parseLong(roleId.toString()));
        return ResponseResult.success(list);
    }


    /**
     * 删除角色
     * @param roleIds
     * @return
     */
    @DeleteMapping("/{roleIds}")
    public ResponseResult delete(@PathVariable("roleIds") String  roleIds) {
        roleService.delete(roleIds);
        return ResponseResult.success();
    }


    /**
     * 更新角色
     * @param roleDto
     * @return
     */
    @ApiOperation(value = "角色修改",notes = "角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState","roleDto.dataScope","roleDto.checkedResourceNos","roleDto.checkedDeptNos","roleDto.id"})
    @PatchMapping
    public ResponseResult update(@RequestBody RoleDto roleDto ){
        Boolean flag = roleService.update(roleDto);
        return ResponseResult.success(flag);
    }
}
